package org.chen.org.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.chen.bas.util.DistanceUtil;
import org.chen.bas.util.Point;
import org.chen.org.domain.Employee;
import org.chen.org.domain.Shop;
import org.chen.org.domain.ShopEmployee;
import org.chen.org.mapper.EmployeeMapper;
import org.chen.org.mapper.ShopEmployeeMapper;
import org.chen.org.mapper.ShopMapper;
import org.chen.org.service.IShopService;
import org.chen.bas.service.lmpl.BasServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chen
 * @since 2024-06-05
 */
@Service
public class ShopServiceImpl extends BasServiceImpl<Shop> implements IShopService {
    @Autowired
    private EmployeeMapper employeeMapper;

    //写入数据
    @Autowired
    private ShopEmployeeMapper shopEmployeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public void settlement(Shop shop) {
    //校验数据
    checkData(shop);
    //保存店铺数据
    //时间
    shop.setRegisterTime(new Date());
    shop.setState(Shop.STATE_WAIT_AUDIT);
    //把shop的地址值解析成经纬度
        Point point = DistanceUtil.getPoint(shop.getAddress());
        //把经纬度存到shop里面
        shop.setLng(point.getLng());
        shop.setLat(point.getLat());
        super.insert(shop);
    //保存管理员数据
        Employee admin = shop.getAdmin();
        admin.setState(Employee.STATE_WAIT_AUDIT);
        employeeMapper.insert(admin);
        //生成中间表数据
        ShopEmployee shopEmployee = new ShopEmployee();
        shopEmployee.setEmployeeId(admin.getId());
        shopEmployee.setShopId(shop.getId());
        shopEmployee.setIsAdmin(ShopEmployee.IS_ADMIN_TRUE);
        shopEmployeeMapper.insert(shopEmployee);
    }



    private void checkData(Shop shop) {
        //1.1 null检验
        if (shop==null)
            throw new RuntimeException("系统错误！请输入相关信息后再操作");
        if (StringUtils.isEmpty(shop.getName()))
            throw new RuntimeException("系统错误！请输入相关信息后再操作");
        if (shop.getAdmin()==null)
            throw new RuntimeException("系统错误！请输入相关信息后再操作");
        if (StringUtils.isEmpty(shop.getAdmin().getUsername())||
                StringUtils.isEmpty(shop.getAdmin().getPassword())|| StringUtils.isEmpty(shop.getAdmin().getConfirmPassword()))
            throw new RuntimeException("系统错误！请输入相关信息后再操作");
        //1.2 两次密码比对
        if (!shop.getAdmin().getPassword().trim().equals(shop.getAdmin().getConfirmPassword().trim()))
            throw new RuntimeException("两次密码不一致");

        //1.3 店铺是否存在
        Shop shopByDb = shopMapper.loadByName(shop.getName());
        if (shopByDb!=null)
            throw new RuntimeException("该店铺已经存在！");
    }
    @Override
    public void update1(Shop shop) {
        shopMapper.update1(shop.getId());
    }

    @Override
    public void insert1(Shop shop) {
        shopMapper.insert1(shop);
    }

    @Override
    public void insert2(Shop shop) {
        shopMapper.insert2(shop);
    }

    @Override
    public void update2(Shop shop) {
        shopMapper.update2(shop);
    }
}
